package com.system.utils;

import com.order.hit.crossover.platform.common.bean.order.Order;
import com.order.hit.crossover.platform.common.bean.order.OrderState;
import com.order.hit.crossover.platform.common.utils.PFResponse;
import com.system.mapper.StoreValueMapper;
import com.order.hit.crossover.platform.common.bean.Store_Value;
import com.order.hit.crossover.platform.common.dao.OrderDao;
import com.order.hit.crossover.platform.common.dao.StoreValueDao;
import com.order.hit.crossover.platform.common.utils.TimeChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lei
 * @version 0.1
 * @date 2020/8/8
 *
 * 主要用于 新模式以及旧模式下的价值计算对比
 */
@Component
public class CountValue {

    @Autowired
    private StoreValueMapper storeValueMapper;

    /**
     * 计算该商店每天的价值提升
     * @param cusNum 顾客的数量，表示为订单数量
     * @param saleNum 销售量
     * @param turnover 营业额
     * @param storeId 店面的ID
     * @return 新旧模式下的价值
     */
    public static Map<String, Double> countValue(int cusNum, int saleNum, Double turnover, String storeId){
        Map<String, Double> map = new HashMap<>();
        Double newValue = new CountValue().getNewValue(cusNum, saleNum, turnover, storeId);
        Double oldValue = new CountValue().getOldValue(cusNum, saleNum, turnover, storeId);
        map.put("newValue", newValue);
        map.put("oldValue", oldValue);
        return map;
    }

    public Double getNewValue(int cusNum, int saleNum, Double turnover,String storeId){
        String today = new SimpleDateFormat("yyyy.MM.dd.HH").format(new Date());
        List<OrderDao> todatyList = TimeChange.getTodayOrderList(
                OrderDao.getListDao((List<Order>)new RestTemplate().exchange("http://order:18081/orderService/getAllOrderByStoreId/"+storeId, HttpMethod.GET, new HttpEntity<>(new HttpHeaders()), PFResponse.class).getBody().get("data")),
                today);
        int allcusnumber = cusNum;
        int safcustomerBynew = 0;
        int safcustomerByold = cusNum;
        int allsal = saleNum;
        int safsalByold = saleNum;
        int safsalBynew = 0;
        Double alltur = turnover;
        for(OrderDao orderDao : todatyList){
            if (orderDao.getOrderState().equalsIgnoreCase(OrderState.ORDER_SUCCESS.toString())){
                safcustomerBynew = safcustomerBynew + 1;
                safcustomerByold = safcustomerByold -1;
                safsalBynew = safsalBynew + orderDao.getGoodNumber();
                safsalByold = safsalByold - orderDao.getGoodNumber();
            } else {
                allcusnumber = allcusnumber + 1;
                allsal = allsal + orderDao.getGoodNumber();
                alltur = alltur + orderDao.getPrice();
            }
        }
        Double customerValue = getCustomeSatisfaction(allcusnumber, safcustomerBynew, safcustomerByold, allsal, safsalBynew, safsalByold);
        Double saleValue = getSales(allsal, safsalByold+safsalBynew);
        Double profit = getProfit();
        Double getTurValue =getTurnover(alltur, turnover);
        Store_Value store_value = StoreValueDao.getStoreValue(this.storeValueMapper.getByStoreId(storeId));
        return store_value.CalculatedValue(customerValue,profit,getTurValue,saleValue);
    }

    public Double getOldValue(int cusNum, int saleNum, Double turnover,String storeId){
        String today = new SimpleDateFormat("yyyy.MM.dd.HH").format(new Date());
        List<OrderDao> todatyList = TimeChange.getTodayOrderList(
                (List<OrderDao>)new RestTemplate().exchange("http://order:18081/orderService/getAllOrderByStoreId/"+storeId, HttpMethod.GET, new HttpEntity<>(new HttpHeaders()), PFResponse.class).getBody().get("data"),
                today);
        int allcusnumber = cusNum;
        int safcustomerByold = cusNum;
        int allsal = saleNum;
        int safsalByold = saleNum;
        Double alltur = turnover;
        for(OrderDao orderDao : todatyList){
            if (orderDao.getOrderState().equalsIgnoreCase(OrderState.ORDER_SUCCESS.toString())){
                safcustomerByold = safcustomerByold -1;
                safsalByold = safsalByold - orderDao.getGoodNumber();
            } else {
                allcusnumber = allcusnumber + 1;
                allsal = allsal + orderDao.getGoodNumber();
                alltur = alltur + orderDao.getPrice();
            }

        }
        Double customerValue = getCustomeSatisfaction(allcusnumber, 0, safcustomerByold, allsal, 0, safsalByold);
        Double saleValue = getSales(allsal, safsalByold+0);
        Double profit = getProfit();
        Double getTurValue =getTurnover(alltur, turnover);
        Store_Value store_value = StoreValueDao.getStoreValue(this.storeValueMapper.getByStoreId(storeId));
        return store_value.CalculatedValue(customerValue,profit,getTurValue,saleValue);
    }

    public static Double getCustomeSatisfaction(int allcusnumber, int safcustomerBynew, int safcustomerByold, int allsal, int safsalBynew, int safsalByold){
        Double cus1 = 1.0 * safcustomerByold / allcusnumber;
        Double cus2 = 0.5 * safcustomerBynew / allcusnumber;

        Double turnover1 = 1.0 * safsalByold / allsal;
        Double turnover2 = 0.5 * safsalBynew / allsal;
        return (cus1 + cus2 + turnover1 + turnover2) * 50.0;
    }

    public static Double getTurnover(Double alltur, Double safTur){
        return 100.0 * safTur / alltur;
    }

    public static Double getSales(int allsales, int safsales){
        return 100.0 * safsales / allsales;
    }

    public static Double getProfit(){
        return 100.0;
    }

}
